package com.lxs.mall.order.web;

import com.lxs.mall.order.service.OrderService;
import com.lxs.mall.order.vo.OrderConfirmVO;
import com.lxs.mall.order.vo.OrderResponseVO;
import com.lxs.mall.order.vo.OrderSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/toTrade")
    public String  toTrade(Model model){
        OrderConfirmVO confirmVO= orderService.confirmOrder();
        model.addAttribute("confirmVO",confirmVO);
        return "confirm";
    }
    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO VO, Model model, RedirectAttributes redirectAttributes){
        Integer code =0;
        OrderResponseVO responseVO=null;
        try {
            responseVO=orderService.orderSubmit(VO);
            code=responseVO.getCode();
        } catch (Exception e) {
            code=2;
        }
        if(code==0){
            //表示下单成功
            model.addAttribute("responseVO",responseVO);
            return "pay";
        }else{
            System.out.println("code="+code);
            String msg="订单失败";
            if(code==1){
                msg=msg+":重复提交 ";
            }
            if(code==2){
                msg=msg+":锁定库存失败";
            }
            //表示下单操作失败
            redirectAttributes.addAttribute("msg",msg);
            return "redirect:http://order.lxsde.com/toTrade";
        }
    }
}
